package com.hancher.common.base;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.viewbinding.ViewBinding;

import com.hancher.common.androidutil.LogUtil;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * 作者：Hancher
 * 时间：2020/1/2.
 * 邮箱：ytu_shaoweijie@163.com
 * 版本：v1.0
 * <p>
 * 说明：自动绑定布局，自动创建presenter
 */
public abstract class BaseMvpFragment<B extends ViewBinding, T extends BasePresenter> extends BaseFragment<B> {
    protected T presenter;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        try {
            Type superclass = getClass().getGenericSuperclass();
            Class<T> aClass = (Class<T>) ((ParameterizedType) superclass).getActualTypeArguments()[1];
            presenter = aClass.newInstance();
        } catch (Exception e) {
            LogUtil.e(e);
        }
        if (presenter != null) {
            presenter.onCreated(this);
        }
        super.onCreate(savedInstanceState);
    }


    @Override
    public void onStart() {
        super.onStart();
        presenter.onStarted();
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.onResumed();
    }

    @Override
    public void onPause() {
        super.onPause();
        presenter.onPaused();
    }

    @Override
    public void onStop() {
        super.onStop();
        presenter.onStoped();
    }

    @Override
    public void onDestroyView() {
        if (presenter != null) {
            presenter.onDestroyed();
        }
        super.onDestroyView();
    }
}
